package com.study.servletstudy;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description : servlet 测试
 * @Author : guxuemin
 * @Version : V1.0.0
 * @Date : 2021/10/21 21:26
 */
@WebServlet(name = "MyServlet", urlPatterns = {"/my"})
public class MyServlet  implements Servlet {

    private transient ServletConfig servletConfig;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.servletConfig = servletConfig;
        System.out.println("servlet init");
    }

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        String servletName = servletConfig.getServletName();
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.print("<html><head></head>"
        + "<body>Hello from "
        + servletName
        + "</body></html>");

        System.out.println("call servlet service");
    }

    @Override
    public String getServletInfo() {
        System.out.println("get servlet info");
        return "My Servlet study";
    }

    @Override
    public void destroy() {
        System.out.println("servlet destory");
    }
}
